/* @Author: Benny Vermonden c1025695
*/

package views.createViews;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenEigenaarModel;
import core.models.domainModel.Adres;
import core.models.domainModel.Persoon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import controllers.createControllers.ToevoegenEigenaarController;

import javax.swing.LayoutStyle.ComponentPlacement;

public class ToevoegenEigenaarView extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txt_naam;
	private JTextField txt_voornaam;
	private JTextField txt_straat;
	private JTextField txt_huisnummer;
	private JTextField txt_postcode;
	private JTextField txt_gemeente;
	private JTextField txt_land;
	private JTextField txt_telefoonnummer;
	private ToevoegenEigenaarModel _model;
	private JTextField txt_bus;
	private JTextField txt_staat;
	private JTextField txt_geboortedatum;


	public ToevoegenEigenaarView(ToevoegenEigenaarModel model) {
		_model = model;
		setTitle("Eigenaar Toevoegen");
		
		JLabel lblNaam = new JLabel("Naam:");
		
		JLabel lblVoornaam = new JLabel("Voornaam:");
		
		JLabel lblAdres = new JLabel("Straat:");
		
		JLabel lblNewLabel = new JLabel("Huisnummer:");
		
		JLabel lblPostcode = new JLabel("Postcode:");
		
		JLabel lblGemeente = new JLabel("Gemeente:");
		
		JLabel lblLand = new JLabel("Land:");
		
		JLabel lblTelefoonnummer = new JLabel("Telefoonnummer:");
		
		txt_naam = new JTextField();
		txt_naam.setColumns(10);
		txt_naam.grabFocus();
		
		
		txt_voornaam = new JTextField();
		txt_voornaam.setColumns(10);
		
		txt_straat = new JTextField();
		txt_straat.setColumns(10);
		
		txt_huisnummer = new JTextField();
		txt_huisnummer.setColumns(10);
		
		txt_postcode = new JTextField();
		txt_postcode.setColumns(10);
		
		txt_gemeente = new JTextField();
		txt_gemeente.setColumns(10);
		
		txt_land = new JTextField();
		txt_land.setColumns(10);
		
		txt_telefoonnummer = new JTextField();
		txt_telefoonnummer.setColumns(10);
		
		JButton btnToevoegen = new JButton("Toevoegen");
		btnToevoegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				/*if (cmb_typeEigenaar.getSelectedIndex() == 0)
				{
					Adres a = new Adres(txt_straat.getText(), Integer.parseInt(txt_huisnummer.getText()), txt_gemeente.getText(), txt_postcode.getText(), txt_land.getText());
					Maatschappij m = new Maatschappij();
					m.setNaam(txt_naam.getText());
					m.setAdres(a);
					_model.AddMaatschappij(m);
				} else*/
				if(Validatie(ToevoegenEigenaarView.this) == true)
				{	
					Persoon p = new Persoon();
					p.setNaam(txt_naam.getText());
					p.setVoornaam(txt_voornaam.getText());
					p.setGeboortedatum(txt_geboortedatum.getText());
					Adres a = new Adres(txt_straat.getText(), Integer.parseInt(txt_huisnummer.getText()), txt_bus.getText(), txt_gemeente.getText(), txt_postcode.getText(), txt_staat.getText(), txt_land.getText());
					p.setAdres(a);
					_model.setAdres(a);
					_model.setEigenaar(p);
					
					ToevoegenEigenaarController.handleView(_model);
					
					JOptionPane.showMessageDialog(null, "succesvol toegevoegd");
				}
				
			}
		});
		
		
		
		JButton btnAnnuleren = new JButton("Annuleren");
		btnAnnuleren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ToevoegenEigenaarController.closeView();
			}
		});
		
		JLabel lblBus = new JLabel("Bus:");
		
		txt_bus = new JTextField();
		txt_bus.setColumns(10);
		
		JLabel lblStaat = new JLabel("Staat:");
		
		txt_staat = new JTextField();
		txt_staat.setColumns(10);
		
		JLabel lblGeboortedatum = new JLabel("Geboortedatum:");
		
		txt_geboortedatum = new JTextField();
		txt_geboortedatum.setColumns(10);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblAdres, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblGemeente, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblPostcode, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblLand, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblTelefoonnummer))
									.addGap(21)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addComponent(txt_gemeente, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
												.addComponent(txt_staat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addComponent(txt_land, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
												.addComponent(txt_telefoonnummer, GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)))
										.addComponent(txt_postcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(txt_straat, GroupLayout.PREFERRED_SIZE, 263, GroupLayout.PREFERRED_SIZE)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(txt_huisnummer, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
											.addGap(18)
											.addComponent(lblBus)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(txt_bus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(btnToevoegen, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnAnnuleren, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE)))
							.addContainerGap(37, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblStaat)
							.addContainerGap(385, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(115)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
										.addComponent(txt_voornaam, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
										.addComponent(txt_geboortedatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNaam, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 255, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblVoornaam, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 232, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblGeboortedatum)
									.addPreferredGap(ComponentPlacement.RELATED, 222, GroupLayout.PREFERRED_SIZE)))
							.addContainerGap(114, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(24)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNaam)
						.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVoornaam)
						.addComponent(txt_voornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGeboortedatum)
						.addComponent(txt_geboortedatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAdres)
						.addComponent(txt_straat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(txt_huisnummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblBus)
						.addComponent(txt_bus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPostcode)
						.addComponent(txt_postcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGemeente)
						.addComponent(txt_gemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblStaat)
						.addComponent(txt_staat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblLand)
						.addComponent(txt_land, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTelefoonnummer)
						.addComponent(txt_telefoonnummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnToevoegen)
						.addComponent(btnAnnuleren))
					.addContainerGap())
		);
		getContentPane().setLayout(groupLayout);
	}
	
	public boolean Validatie(ToevoegenEigenaarView scherm)
	{
		if (txt_naam.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_naam, "naam");
			return false;
		}
		if (txt_voornaam.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_voornaam, "voornaam");
			return false;
		}
		
		if (txt_staat.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_staat, "staat");
			return false;
		}
		
		if (txt_geboortedatum.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_geboortedatum, "geboortedatum");
			return false;
		}
		
		if (txt_huisnummer.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_huisnummer,"huisnummer");	
			return false;
		}
		
		if (txt_gemeente.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_gemeente,"gemeente");
			return false;
		}
		
		if (txt_land.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_land,"land");
			return false;
		}
		
		if (txt_postcode.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_postcode,"postcode");
			return false;
		}
		
		if (txt_straat.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_straat,"straat");
			return false;
		}
		
		if (txt_telefoonnummer.getText().isEmpty())
		{
			ValidatieService.isPresent(scherm.txt_telefoonnummer,"telefoonnummer");
			return false;
		}
		
		if (txt_postcode.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_postcode.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_postcode, "postcode");
				return false;
			}
		}
		
		if (txt_huisnummer.getText() != "")
		{
			try
			{
				Integer.parseInt(scherm.txt_huisnummer.getText());
			}
			catch (NumberFormatException nfe)
			{
				ValidatieService.isInteger(scherm.txt_huisnummer, "huisnummer");
				return false;
			}
		}
		
		return true;
	}
}
